//获取应用实例
// var QQMapWX = require('../../qqmap-wx-jssdk/qqmap-wx.js');

var app = getApp();
// 初始化数据
var init = {

  // 数据
  data: {

    // 数据加载
    loading: true,

  },

  // 页面第一次加载
  onLoad: function (options) {
    var that = this;
    this.setData({
      options: options
    })
    // 适应多种页面传参
    var referer = '';
    for (var i in options) {
      referer += '&' + i + '=' + options[i];
    }
    that.setData({
      referer: referer.substr(1)
    })
    that.getData();
  },

  // 每次加载都调用
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData: function (fn) {

    var that = this;
    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'my_address',
          act: 'index',
          t_session: session,
          store_id: that.data.options.store_id != undefined ? that.data.options.store_id:''
        },

        // 请求成功
        success: function (res) {
          console.log(res.data);
          var data = res.data;

          if (data.done && data.retval) { // 成功
            that.setData({

              // 关闭loading效果
              loading: false,

              // 地址列表
              addrItems: data.retval

            });

            // 成功回调
            typeof fn == 'function' && fn(res);

          } else { // 失败
            that.setData({
              errorTips: data.msg || '请求错误，act：index'
            })
          }

        },

        // 地址列表
        fail: function () {
          that.setData({
            errorTips: '接口请求失败，act：index'
          })
        }
      })
    })

  },

  // 设为默认地址
  setDefault: function (event) {
    var that = this;
    var id = event.currentTarget.dataset.id;
    var status = event.currentTarget.dataset.status;
    var options = that.data.options;
    if (status == 1) return;

    app.getLoginSession(function (session) {
      wx: wx.request({
        url: app.api,
        data: {
          addr_id: id,
          app: 'my_address',
          act: 'site_status',
          t_session: session
        },
        success: function (res) {
          console.log(res);
          if (res.data.done) {
            that.getData(function (result) {
              console.log(result);

              // 适应多种页面传参
              var referer = '';
              for (var i in options) {
                referer += '&' + i + '=' + options[i];
              }
              that.setData({
                referer: referer.substr(1)
              })
              // tabBar来源页面只能用switchTab方法跳转回去，后面不能带参数
              if (options['from'] == 'userCenter') {
                wx.switchTab({
                  url: '/pages/' + options['from'] + '/' + options['from']
                })
              } else {
                console.log('/pages/' + options['from'] + '/' + options['from'] + '?' + referer.substr(1) + '&random=' + Math.random() + that.data.referer);
                wx.redirectTo({
                  url: '/pages/' + options['from'] + '/' + options['from']  + '?' + referer.substr(1) + '&random=' + Math.random()
                })
              }
            });
          }
        }
      })
    })
  },

  // 删除地址
  deleteAddress: function (event) {
    var that = this;
    var id = event.currentTarget.dataset.id;
    console.log(event);
    // 获取用户信息
    app.getLoginSession(function (session) {
      wx.request({
        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'my_address',
          act: 'drop_address',
          t_session: session,
          addr_id: id
        },

        // 请求成功
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.done) {
            that.getData();
          }
        }
      })

    })
  },

  // 编辑地址，地址带上随机数，防止目标页面缓存住后onShow拿不到options
  editAddress: function (event) {
    var that = this;
    var params = event.currentTarget.dataset.from;
    var options = event.currentTarget.dataset.params;
    wx.redirectTo({
      url: '/pages/editAddress/editAddress?' + options
    })
  },
  //不在配送范围
  no_send:function(){
    wx.showModal({
      title: '温馨提示',
      content: '该地址不在商家配送范围，亲，请确认您的收货地址',
      confirmText:'确认地址',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/editAddress/editAddress'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }

};

// 注册页面
Page(init);
